<script setup lang="ts"> 

import { ref, onMounted } from 'vue'
import type { Music } from '@/stores/player'
import { getMusicLibrary, getMusicById }from '@/api/music'
import { usePlayerStore } from '@/stores/player'
import { formatTime } from '@/utils/usertools'
import { ElMessage } from 'element-plus'

const playerStore = usePlayerStore()
const musicList = ref<Music[]>([])
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(10)

// 添加选中状态管理
const selectedSongs = ref<Set<string>>(new Set())

const fetchMusicList = async () => {
  try {
    const result = await getMusicLibrary(currentPage.value, pageSize.value)
    
    if (result.success && result.data) {
      musicList.value = result.data.list
      total.value = result.data.total
    }
  } catch (error) {
    console.error('获取音乐列表失败:', error)
  }
}

const handlePageChange = (page: number) => {
  currentPage.value = page
  fetchMusicList()
}

onMounted(() => {
  fetchMusicList()
})

const playMusic = async (music: Music) => {
  try {
    const musicDetail = await getMusicById(music.id)
    await playerStore.playMusic(musicDetail)
    playerStore.isPlaying = true
  } catch (error) {
    console.error('播放音乐失败:', error)
  }
}

// 处理选中/取消选中
const toggleSelect = (music: Music, event: Event) => {
  event.stopPropagation()
  if (selectedSongs.value.has(music.id)) {
    selectedSongs.value.delete(music.id)
  } else {
    selectedSongs.value.add(music.id)
  }
}

const emit = defineEmits<{
  (e: 'add-to-playlist', songIds: string[]): void
}>()

// 修改添加到歌单的处理函数
const handleAddClick = (event: Event) => {
  event.stopPropagation()
  if (selectedSongs.value.size === 0) {
    ElMessage.warning('请先选择歌曲')
    return
  }
  emit('add-to-playlist', Array.from(selectedSongs.value))
}

// 移除原来的模态框相关代码
const showAddToPlaylistModal = ref(false)
const handleAddSuccess = () => {
  selectedSongs.value.clear()
}

// 添加默认导出
defineOptions({
  name: 'MusicList'
})
</script>

<template>
  <section class="music-list">
    <div class="list-header">
      <h2 class="section-title">音乐库</h2>
      <button 
        class="add-btn"
        :class="{ 'disabled': selectedSongs.size === 0 }"
        @click="handleAddClick"
      >
        <i class="iconfont icon-add"></i>
        添加选中歌曲
      </button>
    </div>
    <div class="list-container">
      <table>
        <thead>
          <tr>
            <th style="width: 50px"></th>
            <th style="width: 80px">封面</th>
            <th style="width: 30%">标题</th>
            <th style="width: 20%">歌手</th>
            <th style="width: 100px">时长</th>
            <th style="width: 120px">操作</th>
          </tr>
        </thead>
        <tbody>
          <tr 
            v-for="music in musicList" 
            :key="music.id"
            class="music-item"
            :class="{ 'selected': selectedSongs.has(music.id) }"
          >
            <td>
              <div 
                class="checkbox"
                @click.stop="toggleSelect(music, $event)"
              >
                <div class="checkbox-inner"></div>
              </div>
            </td>
            <td>
              <div class="cover-wrapper">
                <img :src="music.cover" :alt="music.title" class="cover">
                <div class="play-icon">
                  <i class="iconfont icon-play"></i>
                </div>
              </div>
            </td>
            <td class="song-title">{{ music.title }}</td>
            <td class="artist">{{ music.artist }}</td>
            <td class="duration">{{ formatTime(Number(music.duration)) }}</td>
            <td class="actions">
              <button class="btn play-btn" @click.stop="playMusic(music)">
                <i class="iconfont icon-play"></i>
                播放
              </button>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    <div class="pagination">
      <button 
        class="page-btn"
        :disabled="currentPage === 1"
        @click="handlePageChange(currentPage - 1)"
      >
        <i class="iconfont icon-left"></i>
        上一页
      </button>
      <span class="page-info">{{ currentPage }} / {{ Math.ceil(total / pageSize) }}</span>
      <button 
        class="page-btn"
        :disabled="currentPage >= Math.ceil(total / pageSize)"
        @click="handlePageChange(currentPage + 1)"
      >
        下一页
        <i class="iconfont icon-right"></i>
      </button>
    </div>
  </section>
</template>

<style scoped lang="scss">
.music-list {
  width: 100%;
  padding: 20px;
  background: color-mix(in srgb, var(--bgc-color-2) 40%, transparent);
  border-radius: 12px;
  border: 1px solid color-mix(in srgb, var(--theme-color) 10%, transparent);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  backdrop-filter: blur(12px);

  .list-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
  }

  .section-title {
    color: var(--primary-color);
    font-size: 1.8em;
    margin-bottom: 28px;
    font-weight: 600;
    letter-spacing: 0.5px;
    background: linear-gradient(to right, var(--primary-color), var(--theme-color));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    position: relative;
    display: inline-block;
    text-align: left;

    &::after {
      content: '';
      position: absolute;
      bottom: -8px;
      left: 0;
      width: 60px;
      height: 3px;
      background: linear-gradient(to right, var(--theme-color), transparent);
      border-radius: 2px;
    }
  }

  .list-container {
    width: 100%;
    overflow-x: auto;
  }

  table {
    width: 100%;
    border-spacing: 0;
    border-collapse: separate;

    thead tr {
      th {
        padding: 16px;
        text-align: left;
        color: color-mix(in srgb, var(--primary-color) 90%, transparent);
        border-bottom: 1px solid color-mix(in srgb, var(--theme-color) 10%, transparent);
        font-weight: 500;
        white-space: nowrap;

        &:nth-child(1) { width: 50px; }
        &:nth-child(2) { width: 80px; }
        &:nth-child(3) { width: 30%; }
        &:nth-child(4) { width: 20%; }
        &:nth-child(5) { width: 100px; }
        &:nth-child(6) { 
          width: 120px;
          text-align: center;
        }
      }
    }
  }

  td {
    padding: 0 16px;
    border-bottom: 1px solid color-mix(in srgb, var(--theme-color) 10%, transparent);
    color: var(--primary-color);
    vertical-align: middle;
    height: 72px;
  }

  .music-item {
    background: color-mix(in srgb, var(--bgc-color-2) 30%, transparent);
    border: 1px solid color-mix(in srgb, var(--theme-color) 10%, transparent);
    transition: all 0.2s ease;
    height: 72px;
  }

  .music-item:hover {
    background: color-mix(in srgb, var(--theme-color) 10%, transparent);
    border-color: color-mix(in srgb, var(--theme-color) 20%, transparent);
  }

  .cover-wrapper {
    position: relative;
    width: 48px;
    height: 48px;
    border-radius: 8px;
    overflow: hidden;
  }

  .cover {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  .play-icon {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: rgba(0, 0, 0, 0.4);
    opacity: 0;
    transition: opacity 0.2s ease;
  }

  .cover-wrapper:hover .play-icon {
    opacity: 1;
  }

  .actions {
    display: flex;
    gap: 8px;
    justify-content: center;
    align-items: center;
    min-width: 120px;
  }

  .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 8px 16px;
    border-radius: 6px;
    border: none;
    font-size: 14px;
    cursor: pointer;
    transition: all 0.2s ease;
    white-space: nowrap;
    opacity: 0.8;
  }

  .music-item:hover .btn {
    opacity: 1;
  }

  .play-btn {
    background: color-mix(in srgb, var(--theme-color) 10%, transparent);
    color: var(--primary-color);
    border: 1px solid color-mix(in srgb, var(--theme-color) 20%, transparent);
    width: 90px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    margin: 0;
    
    i {
      font-size: 14px;
      line-height: 1;
    }
  }

  .add-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    border-radius: 20px;
    background: var(--theme-color);
    color: white;
    border: none;
    cursor: pointer;
    transition: all 0.2s;
    margin-left: auto;

    &:hover:not(.disabled) {
      background: var(--secondary-color);
      transform: translateY(-2px);
    }

    &.disabled {
      opacity: 0.5;
      cursor: not-allowed;
    }

    .iconfont {
      font-size: 16px;
    }
  }

  .pagination {
    margin-top: 24px;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 16px;
  }

  .page-btn {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 8px 16px;
    border-radius: 6px;
    background-color: var(--container-background);
    border: 1px solid color-mix(in srgb, var(--theme-color) 20%, transparent);
    color: var(--primary-color);
    cursor: pointer;
    transition: all 0.2s ease;
  }

  .page-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }

  .page-btn:not(:disabled):hover {
    background-color: color-mix(in srgb, var(--bgc-color-2) 40%, transparent);
  }

  .page-info {
    color: var(--secondary-color);
    font-size: 14px;
  }

  .checkbox {
    width: 20px;
    height: 20px;
    border: 2px solid color-mix(in srgb, var(--theme-color) 20%, transparent);
    border-radius: 4px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
  }

  .selected .checkbox {
    background: var(--theme-color);
    border-color: var(--theme-color);
  }

  .checkbox-inner {
    width: 10px;
    height: 10px;
    background: var(--primary-color);
    border-radius: 2px;
    opacity: 0;
    transform: scale(0.5);
    transition: all 0.2s;
  }

  .selected .checkbox-inner {
    opacity: 1;
    transform: scale(1);
  }

  .music-item.selected {
    background: color-mix(in srgb, var(--theme-color) 10%, transparent);
  }

  .music-item.selected:hover {
    background: color-mix(in srgb, var(--theme-color) 15%, transparent);
  }
}
</style> 